# schedules/serializers.py
from rest_framework import serializers
from .models import TimeSlot
from django.utils.translation import gettext_lazy as _

class TimeSlotSerializer(serializers.ModelSerializer):
    period_display = serializers.CharField(
        source='get_period_display',
        read_only=True,
        label=_('时段显示')
    )
    status_display = serializers.CharField(
        source='get_status_display',
        read_only=True,
        label=_('状态显示')
    )
    chef_name = serializers.CharField(
        source='chef.nickname',
        read_only=True,
        label=_('厨师姓名')
    )

    class Meta:
        model = TimeSlot
        fields = [
            'id', 'date', 'period', 'period_display',
            'status', 'status_display', 'chef', 'chef_name',
            'created_at', 'updated_at'
        ]
        read_only_fields = ['id', 'chef', 'created_at', 'updated_at']
        extra_kwargs = {
            'date': {'format': '%Y-%m-%d'},  # 统一日期格式
            'period': {'help_text': _('lunch: 午餐, dinner: 晚餐')},
            'status': {'help_text': _('available: 可用, booked: 已预订, unavailable: 不可用')}
        }

class TimeSlotUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = TimeSlot
        fields = ['status']
        validators = [
            serializers.UniqueTogetherValidator(
                queryset=TimeSlot.objects.all(),
                fields=('chef', 'date', 'period'),
                message=_('该时段已存在')
        )]

class BatchTimeSlotSerializer(serializers.Serializer):
    start_date = serializers.DateField(label=_('开始日期'))
    end_date = serializers.DateField(label=_('结束日期'))
    periods = serializers.MultipleChoiceField(
        choices=TimeSlot.PERIOD_CHOICES,
        label=_('时段列表')
    )

    def validate(self, data):
        if data['start_date'] > data['end_date']:
            raise serializers.ValidationError(_('结束日期不能早于开始日期'))
        return data